/**
 * May 17, 20148:41:23 AM
 */
package elitech.vietnam.copyweatherfashion.fragments.settings;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.adapters.FriendListAdapter;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.dialog.WarningDialogFragment;
import elitech.vietnam.copyweatherfashion.entities.Member;
import elitech.vietnam.copyweatherfashion.fragments.MyAbstractFragment;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 *
 */
public class FriendListContentFragment extends MyAbstractFragment implements OnItemClickListener {

	private static final String TAG = "CONTENT_TAG";
	
	String mTag = "";
	List<Member> mData;
	
	ListView mList;
	FriendListAdapter mAdapter;
	
	MainActivity mActivity;
	
	public static Bundle newArs(String tag) {
		Bundle bundle = new Bundle();
		bundle.putString(TAG, tag);
		return bundle;
	}
	
	public FriendListContentFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mTag = getArguments().getString(TAG);
		mData = (mTag.equals(FriendListFragment.TAG_FOLLOW)) ? mActivity.mFriendList1 : mActivity.mFriendList2;
		View view = inflater.inflate(R.layout.settings_friendlistcontent, container, false);
		
		mList = (ListView) view.findViewById(R.id.mainLayout);
		mAdapter = new FriendListAdapter(mActivity, R.layout.settings_friendlistitem, mData, mTag);
		mList.setAdapter(mAdapter);
		
		mList.setOnItemClickListener(this);
		
		return view;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if (mData.isEmpty())
			new GetData().execute();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
	}
	
	class GetData extends AsyncTask<Integer, Integer, List<Member>> {
		@Override
		protected void onPreExecute() {
			showLoading();
		}
		@Override
		protected List<Member> doInBackground(Integer... params) {
			/*
			 * Integer.MAX_VALUE should be changed to implement load more listener
			 */
			try {
				if (mTag.equals(FriendListFragment.TAG_FOLLOW))
					return new Service().getFollower(mActivity.mUser.Id, 0, Integer.MAX_VALUE);
				else
					return new Service().getFollowing(mActivity.mUser.Id, 0, Integer.MAX_VALUE);
			} catch (IOException | XmlPullParserException e) {
				return null;
			}
		}
		@Override
		protected void onPostExecute(List<Member> result) {
			if (!isVisible())
				return;
			if (result == null) {
				new WarningDialogFragment.Builder()
				.setMessage(getString(R.string.cannot_connect_to_server))
				.build().show(getFragmentManager());
			} else if (result.isEmpty()) {
				showText(getString(R.string.nodata));
			} else {
				mData.clear();
				mData.addAll(result);
				mAdapter.notifyDataSetChanged();
				showMain();
			}
		}
	}
	
	class LoadMoreData extends AsyncTask<Integer, Integer, List<Member>> {
		@Override
		protected List<Member> doInBackground(Integer... params) {
			try {
				if (mTag.equals(FriendListFragment.TAG_FOLLOW))
					return new Service().getFollower(mActivity.mUser.Id, mData.size(), Const.LOADMORE_COUNT);
				else
					return new Service().getFollowing(mActivity.mUser.Id, mData.size(), Const.LOADMORE_COUNT);
			} catch (IOException | XmlPullParserException e) {
				return null;
			}
		}
		@Override
		protected void onPostExecute(List<Member> result) {
			if (!isVisible())
				return;
			if (result == null) {
				new WarningDialogFragment.Builder()
				.setMessage(getString(R.string.cannot_connect_to_server))
				.build().show(getFragmentManager());
			} else if (result.isEmpty()) {
				showText(getString(R.string.nodata));
			} else {
				mData.addAll(result);
				mAdapter.notifyDataSetChanged();
			}
		}
	}
}
